// ----------------------------------------------------------------------------
// Copyright 2006, Martin D. Flynn
// All rights reserved
// ----------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Description:
//  This class provides GUI error and confirmation alert screens.
// ----------------------------------------------------------------------------
// Change History:
//  2006/03/26  Martin D. Flynn
//      Initial release
//  2006/03/31  Martin D. Flynn
//      Placed in custom sub-pakage to allow device dependent UI packaging
// ----------------------------------------------------------------------------
package org.opendmtp.j2me.client.custom.treo650;

import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;

/**
 * The AlertScreen class used for alert messages.
 * 
 * @author Martin D. Flynn
 * @author Elayne Man
 * 
 */
public class AlertScreen extends Alert {

  /**
   * AlertScreen instance.
   */
  private static AlertScreen alertScreen = null;

  /**
   * Shows an alert.
   * 
   * @param title Title of the alert
   * @param msg Message of the alert
   * @param type Type of alert
   * @param visMS The visMS value
   * @param nextScr The next screen
   * @return If successful.
   */
  private static boolean _showAlert(String title, String msg, AlertType type, int visMS,
      Displayable nextScr) {

    /* initialize alert display */
    if (AlertScreen.alertScreen == null) {
      try {
        AlertScreen.alertScreen = new AlertScreen();
      }
      catch (Throwable t) {
        return false;
      }
    }

    /* display alert */
    try {
      return AlertScreen.alertScreen.show(title, msg, type, visMS, nextScr);
    }
    catch (Throwable t) {
      return false;
    }

  }

  /**
   * Shows an error.
   * 
   * @param title Title of error
   * @param msg Message of error
   */
  public static void showError(String title, String msg) {
    AlertScreen._showAlert(title, msg, AlertType.ERROR, Alert.FOREVER, null);
  }

  /**
   * Shows an error.
   * 
   * @param title Title of error
   * @param msg Message of error
   * @param nextScr The next screen
   */
  public static void showError(String title, String msg, Displayable nextScr) {
    AlertScreen._showAlert(title, msg, AlertType.ERROR, Alert.FOREVER, nextScr);
  }

  /**
   * Shows confirmation.
   * 
   * @param title Title of confirmation
   * @param msg Message of confirmation
   * @return The input value of the confirmatoin
   */
  public static boolean showConfirm(String title, String msg) {
    return AlertScreen._showAlert(title, msg, AlertType.CONFIRMATION, Alert.FOREVER, null);
  }

  /**
   * Show confirmation.
   * 
   * @param title Title of confirmation
   * @param msg Message of confirmation
   * @param nextScr The next screen
   * @return The input value of the confirmation
   */
  public static boolean showConfirm(String title, String msg, Displayable nextScr) {
    return AlertScreen._showAlert(title, msg, AlertType.CONFIRMATION, Alert.FOREVER, nextScr);
  }

  /**
   * The command used to OK an alert.
   */
  private Command alertOK = null;

  /**
   * The command used to Cancel an alert.
   */
  private Command alertCancel = null;

  /**
   * The value of the confirmation.
   */
  private boolean alertConfirm = false;

  /**
   * Constructor for AlertScreen.
   * 
   */
  public AlertScreen() {
    super(""); // title is reset later
    this.alertOK = new Command("OK", Command.OK, 1);
    this.alertCancel = new Command("Cancel", Command.CANCEL, 1);
    this.setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable d) {
        AlertScreen.this.alertConfirm = (c == AlertScreen.this.alertOK);
        // Main.showGPSDisplayable(); <-- should not be necessary
      }
    });
  }

  /**
   * Shows the alert.
   * 
   * @param title Title of the alert
   * @param msg Message of the alert
   * @param type Type of alert
   * @param visMS The visMS value
   * @return If successful.
   */
  public boolean show(String title, String msg, AlertType type, int visMS) {
    return this.show(title, msg, type, visMS, null);
  }

  /**
   * Shows the alert.
   * 
   * @param title Title of the alert
   * @param msg Message of the alert
   * @param type Type of alert
   * @param visMS The visMS value
   * @param nextScr The next screen
   * @return If successful.
   */
  public boolean show(String title, String msg, AlertType type, int visMS, Displayable nextScr) {
    try {
      this.setTitle(title);
      this.setString(msg);
      this.setType(type);
      this.setTimeout(visMS);
      if (type == AlertType.CONFIRMATION) {
        this.addCommand(this.alertOK);
        this.addCommand(this.alertCancel);
      }
      else {
        this.removeCommand(this.alertOK);
        this.removeCommand(this.alertCancel);
        // whats left is a dismissal button
      }
      this.alertConfirm = false;

      Main.getDisplay().setCurrent(this, ((nextScr != null) ? nextScr : Main.getGPSDisplayable()));
      return this.alertConfirm;
    }
    catch (Throwable t) {
      return false;
    }
  }

}
